/*=============================================================================

  MANAGER IDLE STATE MODULE

  FILE: SYSmgr_S_MANUAL_READ.cpp

  DESCRIPTION

    Manager Idle State Implementation

  EXTERNALIZED FUNCTIONS


  Copyright (c) DaeSung Engineering Inc. All rights reserved.

=============================================================================*/
/*=============================================================================

  EDIT HISTORY

  ---------  -------  -------------------------------------------------------
  when       who     what, where, why
  ---------  -------  -------------------------------------------------------
  06/28/03   nso4th   Version 0.1

=============================================================================*/

/*=============================================================================
  HEADER INCLUSION
=============================================================================*/
#include <SYSmgr.h>

/*=============================================================================
CONSTANT DEFINITION
=============================================================================*/

/*=============================================================================
LOCALIZED VARIABLE DEFINITION
=============================================================================*/
static dword	gStep = STATE_INIT_STEP;
#define SETSTEP(s)		((gStep) = (s))
#define GETSTEP()		(gStep)

typedef enum
{
	READ_INIT_STEP = STATE_INIT_STEP,

	READ_CONFIG_STEP,
	
	READ_REQ_STEP,
	READ_WAIT_STEP,

	READ_ERR_STEP,
	READ_EXIT_STEP
} MANUAL_READ_Step_e;

/*=============================================================================
EVENT PROCESSING
=============================================================================*/
dword SYSmgr::MANUAL_ProcREAD(dword dwType, dword dwEvent, dword wParam, dword lParam)
{
	dword			evtID, event;
	HFgDev			*fg = m_pFg;
	SYSmgr_cfg_s		*cfg = m_cfg;
	static boolean		sysErr = FALSE;

	event = HDEV_GETEVENT(dwEvent);
	evtID = HDEV_GETDEV_ID(dwEvent);

	// [Internal Command Response]
	if(IS_CMD_EVENT(dwType))
	{
		switch(dwEvent)
		{
		case STATE_STOP_C:
		{
			SETSTEP(READ_EXIT_STEP);
			break;
		}
		}
	}
	else if(dwType == SYS_MGR)
	{
		return	FALSE;
	}

	// [Sequence Control]
	switch(GETSTEP())
	{
	case READ_INIT_STEP:
	{
		area_mode_set(m_areaMod, AREA_READ_M);
		SETSTEP(READ_CONFIG_STEP);
		break;
	}
	case READ_CONFIG_STEP:
	{
		if(fg->IsDeviceAvail() == FALSE)
		{
			ERR_REPORT("READ", "Device Error !!");
			SETSTEP(READ_ERR_STEP);
			break;
		}
		SETSTEP(READ_REQ_STEP);
		break;
	}
	case READ_REQ_STEP:
	{
		//int32		nsec;

		LOGINFO("[READ] Live Image req.");
		
		//---------------------------------------
		// FRAME.GRABBER
		//---------------------------------------
		/*
		nsec = area_exp2nsec(cfg->area_cfg.camEXP);
		if(fg->SetExposureTime(nsec) == FALSE)
		{
			ERR_REPORT("READ", "Exposure time failure !");
			SETSTEP(READ_ERR_STEP);
			break;
		}
		*/
		if(fg->GrabSync(TRUE,NULL) == FALSE)
		{
			ERR_REPORT("READ", "Image req failed..");
			SETSTEP(READ_ERR_STEP);
			break;
		}
		SETSTEP(READ_WAIT_STEP);
		break;
	}
	case READ_WAIT_STEP:
	{
		// just wait
		break;
	}
	case READ_ERR_STEP:
	{
		sysErr = TRUE;
		SETSTEP(READ_EXIT_STEP);
		break;
	}
	case READ_EXIT_STEP:
	default:
	{
		SETSTEP(STATE_INIT_STEP);

		fg->GrabStop();
		area_mode_set(m_areaMod, AREA_NONE_M);
		if(sysErr)
		{
			sysErr = FALSE;
			State_Change(IDL_S, IDL_ERR_S);
		}
		else
		{
			State_Change(IDL_S, IDL_IDL_S);
		}
		break;
	}
	}

	return	FALSE;
}

/*--- END OF SYSmgr_S_MANUAL_INT.CPP ---*/
